package org.jeecg.modules.userFormula.service.impl;

import org.jeecg.modules.userFormula.entity.UserFormula;
import org.jeecg.modules.userFormula.entity.UserFormulaDetail;
import org.jeecg.modules.userFormula.entity.UserFormulaPortion;
import org.jeecg.modules.userFormula.mapper.UserFormulaDetailMapper;
import org.jeecg.modules.userFormula.mapper.UserFormulaPortionMapper;
import org.jeecg.modules.userFormula.mapper.UserFormulaMapper;
import org.jeecg.modules.userFormula.service.IUserFormulaService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 算法库
 * @Author: jeecg-boot
 * @Date:   2023-02-23
 * @Version: V1.0
 */
@Service
public class UserFormulaServiceImpl extends ServiceImpl<UserFormulaMapper, UserFormula> implements IUserFormulaService {

	@Autowired
	private UserFormulaMapper userFormulaMapper;
	@Autowired
	private UserFormulaDetailMapper userFormulaDetailMapper;
	@Autowired
	private UserFormulaPortionMapper userFormulaPortionMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(UserFormula userFormula, List<UserFormulaDetail> userFormulaDetailList,List<UserFormulaPortion> userFormulaPortionList) {
		userFormulaMapper.insert(userFormula);
		if(userFormulaDetailList!=null && userFormulaDetailList.size()>0) {
			for(UserFormulaDetail entity:userFormulaDetailList) {
				//外键设置
				entity.setFormulaId(userFormula.getId());
				userFormulaDetailMapper.insert(entity);
			}
		}
		if(userFormulaPortionList!=null && userFormulaPortionList.size()>0) {
			for(UserFormulaPortion entity:userFormulaPortionList) {
				//外键设置
				entity.setFormulaId(userFormula.getId());
				userFormulaPortionMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(UserFormula userFormula,List<UserFormulaDetail> userFormulaDetailList,List<UserFormulaPortion> userFormulaPortionList) {
		userFormulaMapper.updateById(userFormula);
		
		//1.先删除子表数据
		userFormulaDetailMapper.deleteByMainId(userFormula.getId().toString());
		userFormulaPortionMapper.deleteByMainId(userFormula.getId().toString());
		
		//2.子表数据重新插入
		if(userFormulaDetailList!=null && userFormulaDetailList.size()>0) {
			for(UserFormulaDetail entity:userFormulaDetailList) {
				//外键设置
				entity.setFormulaId(userFormula.getId());
				userFormulaDetailMapper.insert(entity);
			}
		}
		if(userFormulaPortionList!=null && userFormulaPortionList.size()>0) {
			for(UserFormulaPortion entity:userFormulaPortionList) {
				//外键设置
				entity.setFormulaId(userFormula.getId());
				userFormulaPortionMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		userFormulaDetailMapper.deleteByMainId(id);
		userFormulaPortionMapper.deleteByMainId(id);
		userFormulaMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			userFormulaDetailMapper.deleteByMainId(id.toString());
			userFormulaPortionMapper.deleteByMainId(id.toString());
			userFormulaMapper.deleteById(id);
		}
	}
	
}
